using UnityEngine;
public class Player : RoleInfo
{
    public float moveSpeed = 5f;  // 移动速度
    private CharacterController characterController; //角色控制器
    public bool isDead = false; //是否死亡
    public RoleSelect roleSelect;
    public int weaponID = 0;
    public GunList gunList; //子弹发射器列表
    public float[] fireRate; // 射击频率
    public float FireDTime = 0f; // 下一次射击的时间
    private bool isFiring = false; // 是否正在发射子弹

    new void Start()
    {
        base.Start();
        characterController = GetComponent<CharacterController>();
        coll = GetComponent<Collider>();
        roleSelect.init();
        setGun(0);
    }
    void Update()
    {
        if (isDead) return;

        if (FireDTime <= 0)
        { // 时间到才能发射子弹
            if (isFiring)
            { //左键按下时
                FireDTime = fireRate[weaponID]; //进入冷却时间
                anim.SetTrigger("fire"); //播放射击动画
            }
        }
        else FireDTime -= Time.deltaTime; //倒计时
    }

    public override void SetFire(bool on)
    {
        isFiring = on;
    }

    public override void setGun(int id)
    {
        var gunName = "HG";
        switch (id)
        {
            case 0: gunName = "HG"; break;
            case 1: gunName = "AR"; break;
            case 2: gunName = "BZK"; break;
            case 3: gunName = "GND"; break;
        }
        anim.SetTrigger(gunName); //切换动作
        weaponID = id; //武器ID
        roleSelect.setWeapon(id); //切换武器模型
        gunList.SetGun(id); //切换子弹发射器
    }

    public override void Move(float h, float v)
    {
        if (isDead) return;
        if (h == 0 && v == 0)
        {
            anim.SetInteger("state", 0); //停止移动
            return;
        }
        else anim.SetInteger("state", 1); //在移动时
        // 获取摄像机当前的前方方向
        Vector3 cameraForward = Camera.main.transform.forward;
        cameraForward.y = 0; // 去掉 y 轴的分量
        cameraForward.Normalize(); // 归一化

        // 获取摄像机右侧方向
        Vector3 cameraRight = Camera.main.transform.right;

        // 计算移动方向
        Vector3 moveDirection = cameraForward * v + cameraRight * h;
        moveDirection.Normalize(); // 确保方向向量的单位长度
        // 应用移动
        characterController.Move(moveDirection * moveSpeed * Time.deltaTime);
        if (moveDirection != Vector3.zero)
            transform.rotation = Quaternion.LookRotation(moveDirection, Vector3.up); //面向移动方向
    }

    public override void OnEvent()
    {

        RaycastHit hit; // 射线检测门
        if (Physics.Raycast(transform.position, transform.forward, out hit, openDoorRange))
        {
            string tag = hit.collider.tag;
            Debug.Log("开启检测:" + tag);
            switch (tag)
            {
                case "Door":
                    hit.collider.gameObject.SendMessage("OnDoor"); // 触发门事件
                    break;
                case "Car":
                    hit.collider.gameObject.SendMessage("InCar", this); // 触发上车事件
                    break;
            }
        }
    }

    public override void die() //接收RoleInfo中发送的 die 事件
    {
        anim.SetTrigger("die"); //播放死亡动画
        isDead = true; // 标记为死亡
        coll.enabled = false; // 关闭碰撞
    }
}